﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Globalization;
using Public;

namespace WebsiteAdmin.Wilson.ProInfo.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProUpdate : IHttpHandler
    {
        int proReNum = 0;
        DateTime StartDate;
        DateTime EndDate;
        Model.Product pro = new Model.Product();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int rtn = 0;
            if (Cookie.AdminLoginName != null || Cookie.AdminLoginName != "")
            {
                InitDate(context, pro);
                rtn = DoUpdate(Global.ConnWilson, pro);
                if(rtn == 1)
                {
                    int rtn1 = 0;
                    if (proReNum == 1)
                    {
                        rtn1 = UpdateRe(Global.ConnWilson, pro.PKID, StartDate, EndDate, 1);
                    }
                    else
                    {
                        rtn1 = UpdateRe(Global.ConnWilson, pro.PKID, StartDate, EndDate, 0);
                    }
                    context.Response.Write((rtn1 == 1) ? "{ \"Done\": \"Success\" }" : "{ \"Done\": \"Failed\" }");
                }
            }
            
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void InitDate(HttpContext context,Model.Product pro) 
        {
            pro.PKID = Convert.ToInt32(context.Request["PKID"].Trim(), CultureInfo.InvariantCulture);
            pro.ProName = context.Request["ProName"].Trim();
            pro.Keywords = context.Request["Keywords"].Trim();
            pro.Summary = context.Request["Summary"].Trim();
            pro.Description = context.Request["Description"].Trim();
            pro.ProCode = context.Request["ProCode"].Trim();
            pro.MainImgURL = context.Request["MainImgURL"].Trim();
            pro.BigImgURL = context.Request["BigImgURL"].Trim();
            pro.MediumImgURL = context.Request["MediumImgURL"].Trim();
            pro.PackInfo = context.Request["PackInfo"];
            pro.CatId = Convert.ToInt32(context.Request["CatID"].Trim(), CultureInfo.InvariantCulture);
            pro.SerId = Convert.ToInt32(context.Request["SerID"].Trim(), CultureInfo.InvariantCulture);
            pro.SortBy = Convert.ToInt32(context.Request["SortBy"].Trim(), CultureInfo.InvariantCulture);	//显示顺序
            pro.ProContent = context.Request["ProContent"].Trim();
            pro.ProTypeNum = context.Request["ProTypeNum"].Trim();
            proReNum = Convert.ToInt16(context.Request["ProReNum"],CultureInfo.InvariantCulture);
            StartDate = Convert.ToDateTime(context.Request["StartDate"]);
            EndDate = Convert.ToDateTime(context.Request["EndDate"]);
        }
        
        private int DoUpdate(string strCon,Model.Product pro)
        {
            return Bll.Product.Update(strCon, pro);
        }

        private int UpdateRe(string strCon,int proId,DateTime startdate,DateTime enddate,int valid) 
        {
            return Bll.RecommendPro.Update(strCon, proId, startdate, enddate, valid);
        }

    }
}
